package com.paomi.client.fragment.massage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.login.LoginActivity;
import com.paomi.client.activity.message.MessageListActivity;
import com.paomi.client.activity.message.PraiseListActivity;
import com.paomi.client.activity.message.ReplyMessageActivity;
import com.paomi.client.base.BaseMvpFragment;
import com.paomi.client.fragment.massage.model.UnMyMsgBean;
import com.paomi.client.fragment.massage.presenter.MsgFraPre;
import com.paomi.client.net.BaseCallback;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by pc on 2018/10/25
 * Describe:
 * com.paomi.client.fragment
 */
public class MsgFragment extends BaseMvpFragment<MsgMvpView, MsgFraPre> implements MsgMvpView {
    @BindView(R.id.tv_arrow_1)
    TextView tv_arrow_1;
    @BindView(R.id.tv_arrow_2)
    TextView tv_arrow_2;
    @BindView(R.id.tv_arrow_3)
    TextView tv_arrow_3;
    @BindView(R.id.textview_title)
    TextView textviewTitle;
    @BindView(R.id.msg_num)
    TextView msgNum;
    @BindView(R.id.praise_num)
    TextView praiseNum;
    @BindView(R.id.notify_num)
    TextView notifyNum;

    @BindView(R.id.tv_reply_new)
    TextView tv_reply_new;
    @BindView(R.id.tv_like_new)
    TextView tv_like_new;
    @BindView(R.id.tv_system_new)
    TextView tv_system_new;
    private boolean getRead=false;//回复
    private boolean getRead1=false;//点赞
    private boolean getRead2=false;//系统消息

    public static MsgFragment newInstance(String name) {

        Bundle args = new Bundle();
        args.putString("name", name);
        MsgFragment fragment = new MsgFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initEvent() {
        tv_arrow_1.setTypeface(iconfont);
        tv_arrow_2.setTypeface(iconfont);
        tv_arrow_3.setTypeface(iconfont);
        textviewTitle.setText(getResources().getString(R.string.title_msg));
        getPresenter().loadMsgData();
    }

    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.fragment_msg;
    }

    @Override
    public void onResume() {
        super.onResume();
        getPresenter().loadMsgData();
    }

    @Override
    public MsgFraPre createPresenter() {
        return new MsgFraPre();
    }

    @OnClick({R.id.msg_ll, R.id.praise_ll, R.id.system_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.msg_ll:
                if(getUserInfo()==null){//未登录的判断
                    BaseCallback.showLoginDialog();
                    return;
                }
                Intent intent = new Intent(getActivity(), ReplyMessageActivity.class);
                startActivityForResult(intent, 2002);
                break;
            case R.id.praise_ll:
                if(getUserInfo()==null){//未登录的判断
                    BaseCallback.showLoginDialog();
                    return;
                }
                startActivityForResult(new Intent(getActivity(), PraiseListActivity.class), 2002);
                break;
            case R.id.system_ll:
                if(getUserInfo()==null){//未登录的判断
                    BaseCallback.showLoginDialog();
                    return;
                }
                startActivityForResult(new Intent(getActivity(), MessageListActivity.class), 2002);
                break;
        }
    }

    @Override
    public void setMsgNum(UnMyMsgBean unMyMsgBean) {
        if (unMyMsgBean != null) {
            if (unMyMsgBean.getReturnData().getCommentTotal() > 0) {
                msgNum.setText("" + unMyMsgBean.getReturnData().getCommentTotal());
                if(!getRead){
                    tv_reply_new.setVisibility(View.VISIBLE);
                }else{
                    tv_reply_new.setVisibility(View.GONE);
                }
            } else {
                msgNum.setText("");
                tv_reply_new.setVisibility(View.GONE);
            }
            if (unMyMsgBean.getReturnData().getLikeTotal() > 0) {
                praiseNum.setText("" + unMyMsgBean.getReturnData().getLikeTotal());
                if(!getRead1){
                    tv_like_new.setVisibility(View.VISIBLE);
                }else{
                    tv_like_new.setVisibility(View.GONE);
                }
            } else {
                praiseNum.setText("");
                tv_like_new.setVisibility(View.GONE);
            }
            if (unMyMsgBean.getReturnData().getMessageTotal() > 0) {
                notifyNum.setText("" + unMyMsgBean.getReturnData().getMessageTotal());
                if(!getRead2){
                    tv_system_new.setVisibility(View.VISIBLE);
                }else{
                    tv_system_new.setVisibility(View.GONE);
                }
            } else {
                notifyNum.setText("");
                tv_system_new.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2002) {//查看列表的点击
            if(resultCode==2003 && data.getStringExtra("num")!=null){//回复
                tv_reply_new.setVisibility(View.GONE);
                getRead=true;
                getPresenter().loadMsgData();
            }else if(resultCode==2005 && data.getStringExtra("num1")!=null){//点赞
                tv_like_new.setVisibility(View.GONE);
                getRead1=true;
                getPresenter().loadMsgData();
            }else if(resultCode==2006 && data.getStringExtra("num2")!=null){//系统消息
                getRead2=true;
                getPresenter().loadMsgData();
            }
        }
    }
}
